#!/usr/bin/env python
# -*- encoding: utf8 -*-

import os
import sys
import settings
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
sys.path.append('../')
from reps.interns.models import *


def usage():
    print 'Usage: %s - view all variables' % sys.argv[0]
    print '       %s key - view variable' % sys.argv[0]
    print '       %s key value - change value' % sys.argv[0]
    print
    print '\tkey - variable name'
    print '\tvalue - value to set'


if __name__ == '__main__':
    if len(sys.argv) > 3:
        usage()
        sys.exit(1)

    if len(sys.argv) == 1:
        for setting in Setting.objects.all():
            print setting.key, '=', setting.value
        sys.exit(0)

    key = sys.argv[1]
    change = len(sys.argv) == 3
    if change:
        value = sys.argv[2]
    try:
        setting = Setting.objects.get(key=sys.argv[1])
        if change:
            setting.value = value
            setting.save()
        print setting.key, '=', setting.value
    except:
        if change:
            setting = Setting(key=key, value=value)
            setting.save()
            print setting.key, '=', setting.value
        else:
            print 'Variable "%s" does not exist!' % key

